package org.holynite.fieldmanage.managed.backing

import org.holynite.fieldmanage.managed.model.ChargeRuleFormBean
import org.holynite.fieldmanage.managed.model.ChargeRulesBean

import javax.enterprise.context.RequestScoped
import javax.faces.bean.ManagedBean
import javax.faces.bean.ManagedProperty
import javax.faces.event.ValueChangeEvent

/**
 * Created by midnite on 15/2/1.
 */
@ManagedBean
@RequestScoped
class ChargeRuleSelectBean implements Serializable {

    @ManagedProperty("#{chargeRuleFormBean}")
    ChargeRuleFormBean crForm
    @ManagedProperty("#{chargeRulesBean}")
    ChargeRulesBean crBean

    void loadRuleDetail(ValueChangeEvent event) {
        def selected = event.newValue as String
        if (selected.equals("new_content")) {
            crForm.addMode = true
            crForm.clearForm()
        } else {
            crForm.addMode = false
            def selectedRule = crBean.findRule(selected)
            crForm.ruleName = selectedRule.name
            def cal = Calendar.instance
            cal.setTime(selectedRule.begin)
            crForm.beginHour = cal.get(Calendar.HOUR_OF_DAY)
            crForm.beginMinute = cal.get(Calendar.MINUTE)
            cal.setTime(selectedRule.end)
            crForm.endHour = cal.get(Calendar.HOUR_OF_DAY)
            crForm.endMinute = cal.get(Calendar.MINUTE)
            crForm.price = selectedRule.price
        }
    }
}
